package conponent;

import controller.CollisionUtils;
import module.Direction2D;
import module.EntityType;
import module.Point2D;
import module.Status;
import view.CombatView;
import view.View;

import java.awt.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class PlayerComp extends EntityComp implements Player{
    private HashMap<Status,Image> im;
    /**
     * 该容器存储的知识魔法球的模板，用于实体工厂制造魔法球。
     */
    private List<MagicBallComp> mbl;
    private Status status;
    //移动方向
    private Direction2D moveDir;
    private Thread thread;
    private int moveSpeed;
    public PlayerComp(int x, int y,Image show) {
        super(show,x, y);
        im=new HashMap<>();
        mbl=new ArrayList<MagicBallComp>();
        moveSpeed=32;
        status=Status.STAND_SOUTH;
        thread=new Thread(this);
        thread.start();
    }

    public void addStatusImage(Status st,Image image){
        im.put(st,image);
    }

    @Override
    public EntityType getEntityType() {
        return EntityType.Player;
    }

    @Override
    public Thread getThread() {
        return null;
    }

    @Override
    public Status getStatus() {
        return status;
    }

    @Override
    public Status getSpecialStatus() {
        return null;
    }

    @Override
    public void leftMove() {
        this.status=Status.WALK_WEST;
        this.moveDir=Direction2D.left;
        setShow(im.get(Status.STAND_WEST));
    }

    @Override
    public void rightMove() {
        this.status=Status.WALK_EAST;
        this.moveDir=Direction2D.right;
        setShow(im.get(Status.STAND_EAST));
    }

    @Override
    public void upMove() {
        this.status=Status.WALK_NORTH;
        this.moveDir=Direction2D.up;
        setShow(im.get(Status.STAND_NORTH));
    }

    @Override
    public void downMove() {
        this.status=Status.WALK_SOUTH;
        this.moveDir=Direction2D.down;
        setShow(im.get(Status.STAND_SOUTH));

    }

    @Override
    public void stopMove() {
        this.moveDir=Direction2D.Zero;
        if(this.status==Status.WALK_SOUTH){
            this.status=Status.STAND_SOUTH;
        }else if(this.status==Status.WALK_EAST){
            this.status=Status.STAND_EAST;
        }else if(this.status==Status.WALK_WEST){
            this.status=Status.STAND_WEST;
        }else if(this.status==Status.WALK_NORTH){
            this.status=Status.STAND_NORTH;
        }
    }

    @Override
    public void addMagic(MagicBallComp mb) {
        mbl.add(mb);
        mb.setOwner(this);
    }

    @Override
    public void action(int i) {
        SimpleEntityFactory f = new SimpleEntityFactory();
        MagicBallComp mbc = mbl.get(i);

        MagicBallComp mb = f.createMagicBall(mbc);
        CombatView view =(CombatView) getView();
        mb.beforeCreated();
        System.out.println(mb);
        view.addEntityComp(mb);
    }


    @Override
    public MagicBall getMagicBall(int index) {
        return null;
    }

    @Override
    public void run() {
        while(true){
            if(status==Status.WALK_EAST||status==Status.WALK_WEST||status==Status.WALK_SOUTH||status==Status.WALK_NORTH){
                Point2D vec = moveDir.getVec();
                for(int i=0;i<moveSpeed;i++){
                    translate(vec.getX(),vec.getY());
                    if(CollisionUtils.isCollision(this)){
                        translate(-vec.getX(),-vec.getY());
                        break;
                    }

                }
            }

            try {
                Thread.sleep(125);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
